// 引入 react 依赖
import React, { Component } from 'react';
import { connect } from 'react-redux';
import { Text, View, Image, TouchableOpacity, ImageBackground, ScrollView, InteractionManager } from 'react-native';

// 引入第三方依赖
import _ from 'lodash';
import pureRender from 'pure-render-decorator';
import * as ActionTypes from '../../constants/constants.js'

// antd 按需加载
import { Tabs, Toast, Portal } from '@ant-design/react-native';

// 引入 处理数据 所需文件
import { search } from './module/module.js'

// 引入样式
import Style from './style/style.js';

// 引入公用组件
import { Status, NavBar, RnFlatList, Good } from '../../components/index.js'

// 引入专用组件
import { } from './components'

// 引入工具包
import { press, size, ScreenWidth } from '../../util/utils.js'
class GoodsList extends Component {
  constructor(props) {
    super(props)
    this.state = {
      refreshing: false,
      // // searchBackgroundColor: 'transparent',
      // searchBackgroundColor: '#e43130',
    }
    this.search = search.bind(this)
  }

  componentDidMount() {
    const { keyword, searchList: { nextPage, pageSize } } = this.props
    const loading = Toast.loading('加载中', 0, null, false)
    this.search({
      pageNum: nextPage,
      pageSize,
      keyword
    }).then(res => {
      Portal.remove(loading)
    }).catch(error => {
      Portal.remove(loading)
    })
  }

  componentWillUnmount() {
    const { dispatch } = this.props
    dispatch({
      type: ActionTypes.searchListReset,
    })
  }

  onRefresh = () => {
    const { keyword, searchList: { pageSize } } = this.props
    this.setState({ refreshing: true });
    const loading = Toast.loading('加载中', 0, null, false)
    this.search({
      pageNum: 1,
      pageSize,
      keyword,
      refresh: true
    }).then(res => {
      Portal.remove(loading)
      this.setState({ refreshing: false });
    }).catch(error => {
      Portal.remove(loading)
    })
  }

  onEndReached = () => {
    const { keyword, searchList: { nextPage, pageSize, list, total } } = this.props
    if (list.length < total) {
      this.search({
        pageNum: nextPage,
        pageSize,
        keyword
      })
    }
  }

  render() {
    const { refreshing } = this.state
    const { keyword, searchList } = this.props
    return (
      <View style={Style.container}>
        <Status />
        <NavBar
          title={`搜索 《${keyword}》`}
          back={true}
        />
        <RnFlatList
          style={{
          }}
          columnWrapperStyle={{
            backgroundColor: '#f0f2f5',
            paddingLeft: 3,
            paddingRight: 3,
            paddingTop: 3,
          }}
          // onScroll={this.onScroll}
          refreshing={refreshing}
          onRefresh={this.onRefresh}
          onEndReached={this.onEndReached}
          numColumns={2}
          removeClippedSubviews={true}
          datas={searchList}
          renderItem={({ item }) => <Good type="good" data={item} />}
        />
      </View >
    );
  }
}


export default connect(({ searchList }) => ({
  searchList,
}))(pureRender(GoodsList))
